from django.db import models
from django.utils.safestring import mark_safe

from api.utils import image_to_url


class Banner(models.Model):
    image = models.ImageField(verbose_name=u'图片')
    url = models.URLField(verbose_name=u'链接')
    order = models.PositiveIntegerField(verbose_name=u'顺序')

    @property
    def image_url(self):
        return image_to_url(self.image)

    @property
    def image_display(self):
        if self.image:
            return mark_safe(
                '<a href="%s"><img src="%s" style="max-height: 50px;min-height: 100px" /></a>' % (
                    self.image.url, self.image.url))
        else:
            return '(no image)'
